<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Section 8.2.2: Separating polyhedra in 2D</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch08_geometric_probs/html/separate_poly_2D.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Section 8.2.2: Separating polyhedra in 2D</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Joelle Skaf - 10/09/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% If the two polyhedra C = {x | A1*x &lt;= b1} and D = {y | A2*y &lt;= b2} can be</span>
<span class="comment">% separated by a hyperplane, it will be of the  form</span>
<span class="comment">%           z'*x - z'*y &gt;= -lambda'*b1 - mu'*b2 &gt; 0</span>
<span class="comment">% where z, lambda and mu are the optimal variables of the problem:</span>
<span class="comment">%           maximize    -b1'*lambda - b2'*mu</span>
<span class="comment">%               s.t.    A1'*lambda + z = 0</span>
<span class="comment">%                       A2'*mu  - z = 0</span>
<span class="comment">%                       norm*(z) &lt;= 1</span>
<span class="comment">%                       lambda &gt;=0 , mu &gt;= 0</span>
<span class="comment">% Note: here x is in R^2</span>

<span class="comment">% Input data</span>
randn(<span class="string">'seed'</span>,0);
n  = 2;
m = 2*n;
A1 = [1 1; 1 -1; -1 1; -1 -1];
A2 = [1 0; -1 0; 0 1; 0 -1];
b1 = 2*ones(m,1);
b2 = [5; -3; 4; -2];

<span class="comment">% Solving with CVX</span>
fprintf(1,<span class="string">'Finding a separating hyperplane between the 2 polyhedra...'</span>);

cvx_begin
    variables <span class="string">lam(m)</span> <span class="string">muu(m)</span> <span class="string">z(n)</span>
    maximize ( -b1'*lam - b2'*muu)
    A1'*lam + z == 0;
    A2'*muu - z == 0;
    norm(z) &lt;= 1;
    -lam &lt;=0;
    -muu &lt;=0;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------'</span>);
disp(<span class="string">'The distance between the 2 polyhedra C and D is: '</span> );
disp([<span class="string">'dist(C,D) = '</span> num2str(cvx_optval)]);

<span class="comment">% Plotting</span>
t = linspace(-3,6,100);
p = -z(1)*t/z(2) + (muu'*b2 - lam'*b1)/(2*z(2));
figure;
fill([-2; 0; 2; 0],[0;2;0;-2],<span class="string">'b'</span>, [3;5;5;3],[2;2;4;4],<span class="string">'r'</span>)
axis([-3 6 -3 6])
axis <span class="string">square</span>
hold <span class="string">on</span>;
plot(t,p)
title(<span class="string">'Separating 2 polyhedra by a hyperplane'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Finding a separating hyperplane between the 2 polyhedra... 
Calling sedumi: 12 variables, 5 equality constraints
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 5, order n = 12, dim = 13, blocks = 2
nnz(A) = 18 + 0, nnz(ADA) = 25, nnz(L) = 15
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.26E+01 0.000
  1 :  -1.46E+00 4.21E+00 0.000 0.3333 0.9000 0.9000   2.47  1  1  2.1E+00
  2 :  -2.14E+00 4.21E-01 0.000 0.1000 0.9900 0.9900   1.34  1  1  1.9E-01
  3 :  -2.12E+00 2.81E-02 0.000 0.0668 0.9900 0.9900   1.03  1  1  1.2E-02
  4 :  -2.12E+00 2.77E-03 0.019 0.0986 0.9900 0.9900   1.00  1  1  1.3E-03
  5 :  -2.12E+00 8.19E-06 0.000 0.0030 0.9990 0.9990   1.00  1  1  3.8E-06
  6 :  -2.12E+00 2.62E-07 0.000 0.0320 0.9900 0.9900   1.00  1  1  1.4E-07
  7 :  -2.12E+00 1.11E-08 0.210 0.0422 0.9900 0.5465   1.00  1  2  1.6E-08
  8 :  -2.12E+00 1.66E-10 0.000 0.0150 0.9902 0.9900   1.00  2  2  3.9E-10

iter seconds digits       c*x               b*y
  8      0.1   Inf -2.1213203424e+00 -2.1213203423e+00
|Ax-b| =   2.0e-11, [Ay-c]_+ =   1.3E-09, |x|=  1.9e+00, |y|=  3.9e+00

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    5.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 5,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 2163.65.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +2.12132
Done! 
------------------------------------------------------------------
The distance between the 2 polyhedra C and D is: 
dist(C,D) = 2.1213
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="separate_poly_2D__01.png" alt=""> 
</div>
</div>
</body>
</html>